package com.zhugang.week06;

import java.util.*;

/**
 * @program algorithms
 * @description: levelOrderGroup
 * @author: chanzhugang
 * @create: 2022/07/19 11:17
 */
public class LevelOrderGroupN2 {

    /**
     * 429 N叉树的层序遍历
     *
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrderGroup(Node root) {
        // 层序遍历都用队列实现
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            List<Integer> curLevel = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Node node = queue.poll();
                curLevel.add(node.val);
                for (int j = 0; j < node.children.size(); j++) {
                    queue.add(node.children.get(j));
                }
            }
            result.add(curLevel);
        }
        return result;
    }

    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

}